﻿using System.Text.RegularExpressions;
using Baula.TextGen.Framework.Tools;

namespace Baula.TextGen.Framework.Sections
{
    public class RelocationSection : ISection
    {
        private const string BeginTokenRegexPattern = "' @Section:(.*)";  // TODO: make more flexible

        public static bool IsBegin(string line)
        {
            var regex = new Regex(BeginTokenRegexPattern);

            return regex.IsMatch(line);
        }

        private readonly LineCollector _lineCollector = new LineCollector();
        private readonly string _targetSectionName;

        public RelocationSection(string headingLine)
        {
            _targetSectionName = GetReferredSectionName(headingLine);
        }

        private string GetReferredSectionName(string headingLine)
        {
            var regex = new Regex(BeginTokenRegexPattern);

            return regex.Match(headingLine).Groups[1].Value;
        }

        public string Content
        {
            get { return LineCollector.ToString(); }
        }

        private LineCollector LineCollector
        {
            get { return _lineCollector; }
        }

        public PlaceholderSection RelocateTo { get; set; }

        public string TargetSectionName
        {
            get { return _targetSectionName; }
        }

        public void AppendLine(string line)
        {
            LineCollector.AppendAsNewLine(line);
        }
    }
}